<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Product extends Admin_Controller {
	
	public function __construct(){
        parent::__construct();
		$this->load->model('mod_products');
	}
	
 	public function index(){
		$this->view();	
	}
   
   	public function view(){	
        $config['base_url'] = base_url() . 'product/product/view/';
        $config['total_rows'] = $this->mod_products->getProductNum();
		$config['uri_segment'] = 4;
        $config['per_page'] = 15;

        $this->pagination->initialize($config);
		$data['data'] = $this->mod_products->getProducts($this->uri->segment(4), $config['per_page']);
						
		$data['title']	= 'Products';
		$data['page']	= 'product/view';
		$data['action']	= 'product';
		$data['method'] = 'product';
    	$this->load->view('layout/admin',$data);
   	}
   
	public function add(){
		$action = $this->uri->segment(4);
					
		if($this->input->post()){
        	$this->form_validation->set_rules('name', 'Product Name', 'trim|required');
        	$this->form_validation->set_rules('description', 'Description', 'trim|required');
			$this->form_validation->set_rules('broken_rate', 'Broken Rate', 'trim|required');
			$this->form_validation->set_rules('shipping', 'shipping', 'trim|required');
			$this->form_validation->set_rules('mini_order', 'Mini Order', 'trim|required');
			$this->form_validation->set_rules('payment_term', 'Payment Term', 'trim|required');
			$this->form_validation->set_rules('fob_price', 'Fob Price', 'trim|required');
			
        	if ($this->form_validation->run() == TRUE) {
	
				$config['upload_path'] = PRODUCT_IMAGE_PATH;
				$config['allowed_types'] = 'gif|jpg|jpeg|png|JPEG|JPG|PNG|x-png';
				$config['max_size']	= '1024';
			
				$this->load->library('upload', $config);
		
				if ($this->upload->do_upload('image')){				
					$image = $this->upload->data();
					$imageName = $image['file_name'];
					resize_image($imageName, 120, 160, $config['upload_path']);
						
					$title = $this->input->post('name');
					$broken = $this->input->post('broken_rate');
					$shipping = $this->input->post('shipping');
					$mini_order = $this->input->post('mini_order');
					$payment = $this->input->post('payment_term');
					$fob_price = $this->input->post('fob_price');
					$description = $this->input->post('description');
					
					if($this->mod_products->insert($imageName, $title, $description, $broken, $shipping, $mini_order, $payment, $fob_price) > 0){
						$this->session->set_userdata('ms', 'Successfully add new product!');
						redirect('product/product/');
					} else {
						$this->session->set_userdata('ms', 'Could not add new product, please try again!');
					}
				} else {
					$this->session->set_userdata('ms', $this->upload->display_errors());
				}
			} else {
				$this->session->set_userdata('ms', validation_errors());
			}
		}
		
		$data['title']="Add New product";
		$data['page'] = 'product/new';
		$data['action']='Add New product';
		$data['method'] = $action;
    	$this->load->view('layout/admin',$data);
	}
	
	public function edit(){
		$action = $this->uri->segment(4);
		$pageNumber = $this->uri->segment(5);
		$id = $this->uri->segment(6);
		
		$row = $this->mod_products->getProductById($id);
					
		if($this->input->post()){
        	$this->form_validation->set_rules('name', 'Product Name', 'trim|required');
        	$this->form_validation->set_rules('description', 'Description', 'trim|required');
			$this->form_validation->set_rules('broken', 'Broken Rate', 'trim|required');
			$this->form_validation->set_rules('shipping', 'Shipping', 'trim|required');
			$this->form_validation->set_rules('mini_order', 'Mini Order', 'trim|required');
			$this->form_validation->set_rules('payment', 'Payment Term', 'trim|required');
			$this->form_validation->set_rules('fob', 'Fob Price', 'trim|required');
			
        	if ($this->form_validation->run() == TRUE) {
				$title = $this->input->post('name');
				$broken = $this->input->post('broken');
				$shipping = $this->input->post('shipping');
				$mini_order = $this->input->post('mini_order');
				$payment = $this->input->post('payment');
				$fob_price = $this->input->post('fob');
				$description = $this->input->post('description');
	
				$config['upload_path'] = PRODUCT_IMAGE_PATH;
				$config['allowed_types'] = 'gif|jpg|jpeg|png|JPEG|JPG|PNG|x-png';
				$config['max_size']	= '1024';
			
				$this->load->library('upload', $config);
		
				if ($this->upload->do_upload('image')){				
					$image = $this->upload->data();
					$imageName = $image['file_name'];
					resize_image($imageName, 120, 160, $config['upload_path']);
											
					if($this->mod_products->update($id, $imageName, $title, $description, $broken, $shipping, $mini_order, $payment, $fob_price) > 0){
						$this->session->set_userdata('ms', 'Successfully edit product!');
						delete_file(PRODUCT_IMAGE_PATH . $row[field('image')]);
						delete_file(PRODUCT_IMAGE_PATH . 'thumbs/' . $row[field('image')]);
						redirect('product/product/' . $action . '/' . $pageNumber);
					} else {
						$this->session->set_userdata('ms', 'Could not edit product, please try again!');
					}
				} else {
										
					if($this->mod_products->update($id, '', $title, $description, $broken, $shipping, $mini_order, $payment, $fob_price) > 0){
						$this->session->set_userdata('ms', 'Successfully edit product!');
						redirect('product/product/' . $action . '/' . $pageNumber);
					} else {
						$this->session->set_userdata('ms', 'Could not edit product, please try again!');
					}
				}
			}
		}
		$data['row'] = $row;
		
		$data['title']="Edit product";
		$data['page'] = 'product/edit';
		$data['action']='Edit Product';
		$data['method'] = $action;
    	$this->load->view('layout/admin',$data);
	}
	
	public function delete(){
		$action = $this->uri->segment(4);
		$pageNumber = $this->uri->segment(5);
		$id = $this->uri->segment(6);
		
		$data = $this->mod_products->getProductById($id);
		
        if ($this->mod_products->delete($id) > 0) {
            $this->session->set_userdata('ms', 'Successfully delete!');
			delete_file(PRODUCT_IMAGE_PATH . $data[field('image')]);
			delete_file(PRODUCT_IMAGE_PATH . '/thumbs/' . $data[field('image')]);
        }else{
            $this->session->set_userdata('ms', 'Could not delete, please try again!');
		}
        redirect('product/product/' . $action . '/' . $pageNumber);
	}
}